﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using XTrace;

namespace XTrace_UnitTests
{

	class FakeListener : XTraceListener
	{
		readonly List<XTraceEvent> _events = new List<XTraceEvent>();
		public IEnumerable<XTraceEvent> Events
		{
			get
			{
				lock (_events)
				{
					return _events.ToArray();
				}
			}
		}

		public override void Event(XTraceEvent traceEvent)
		{
			lock (_events)
			{
				_events.Add(traceEvent);
			}
		}
	}

	[TestClass]
	public class Create_complex_logging_path
	{
		[TestMethod]
		public void Should_log_dirrectly_from_one_source_to_one_logger()
		{
			// Setup
			var source1 = new XTraceSource("source1");
			var source2 = new XTraceSource("source2");
			var listener1 = new FakeListener();
			var listener2 = new FakeListener();
			source1.AddBundle(listener1);
			source2.AddBundle(listener2);

			Assert.IsTrue(source1.Bundles.Any());
			Assert.AreEqual(1, source1.Bundles.Count());
			var bundle = source1.Bundles.Single();
			Assert.AreEqual(bundle, listener1.Bundles.Single());
			Assert.AreEqual(source1, bundle.Sources.Single());
			Assert.AreEqual(listener1, bundle.Listeners.Single());

			// Execute
			source1.Information("Test");
			// Verify
			Assert.AreEqual(1, listener1.Events.Count());
			Assert.AreEqual(0, listener2.Events.Count());
		}

	}
}
